package xuegao.study.mall.admin.service.ums.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xuegao.study.mall.admin.feign.UmsAdminClient;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.api.ResultCode;
import xuegao.study.mall.common.dto.rbac.UmsAdminRegisterDto;
import xuegao.study.mall.common.model.ums.*;
import xuegao.study.mall.common.param.ums.PlatformType;
import xuegao.study.mall.common.param.ums.UmsAdminLoginParam;
import xuegao.study.mall.repo.dao.ums.UmsAdminDao;
import xuegao.study.mall.repo.dao.ums.UmsResourceDao;
import xuegao.study.mall.repo.dao.ums.UmsRoleDao;
import xuegao.study.mall.admin.feign.Oauth2Client;
import xuegao.study.mall.admin.service.ums.UmsAdminService;
import java.util.*;

@Service
public class UmsAdminServiceImpl implements UmsAdminService {

    Logger logger = LoggerFactory.getLogger(UmsAdminServiceImpl.class);

    @Autowired
    private UmsAdminClient umsAdminClient;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private UmsAdminDao umsAdminDao;

    @Autowired
    private UmsRoleDao umsRoleDao;

    @Autowired
    private UmsResourceDao umsResourceDao;

    @Autowired
    private Oauth2Client oauth2Client;

    @Value("${oauth2.clientId}")
    private String oauth2ClientId;

    @Value("${oauth2.clientSecret}")
    private String oauth2ClientSecret;

    @Value("${oauth2.callback}")
    private String oauth2Callback;

    @Override
    public CommonResult login(UmsAdminLoginParam param) {
        Integer type = param.getPlatformType();
        if (type == PlatformType.PLATFORM_TYPE_USERNAME) {
            return doLoginByUser(param);
        } else if (type == PlatformType.PLATFORM_TYPE_OAUTH2) {
            return doLoginByCode(param);
        }
        return doLoginByUser(param);
    }

    /**
     * 用户名密码登录
     */
    private CommonResult doLoginByUser(UmsAdminLoginParam param) {
        String username = param.getUsername();
        String password = param.getPassword();
        Map<String, String> params = new HashMap<>();
        params.put("client_id", oauth2ClientId);
        params.put("client_secret", oauth2ClientSecret);
        params.put("grant_type","password");
        params.put("username",username);
        params.put("password",password);
        CommonResult restResult = oauth2Client.getAccessToken(params);
        if (restResult.getCode() == ResultCode.SUCCESS.getCode() && restResult.getData() != null) {
            umsAdminClient.log(username);
        }
        return restResult;
    }

    /**
     * 授权码登录
     */
    private CommonResult doLoginByCode(UmsAdminLoginParam param) {
        String code = param.getCode();
        Map<String, String> params = new HashMap<>();
        params.put("client_id", oauth2ClientId);
        params.put("client_secret", oauth2ClientSecret);
        params.put("grant_type","authorization_code");
        params.put("code", code);
        params.put("redirect_uri", oauth2Callback);
        CommonResult restResult = oauth2Client.getAccessToken(params);
        if (restResult.getCode() == ResultCode.SUCCESS.getCode() && restResult.getData() != null) {
            umsAdminClient.log(code);
        }
        return restResult;
    }

    @Override
    public CommonResult register(UmsAdmin umsAdmin) {
        UmsAdminRegisterDto dto = new UmsAdminRegisterDto();
        BeanUtils.copyProperties(umsAdmin, dto);
        return umsAdminClient.register(dto);
    }
}
